package com.suguowen.sblog.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.suguowen.sblog.bean.Comment;
import com.suguowen.sblog.bean.Tag;
import com.suguowen.sblog.bean.User;
import com.suguowen.sblog.bean.vo.ArticlesVO;
import com.suguowen.sblog.bean.vo.CommonVO;
import com.suguowen.sblog.common.MySpringContext;
import com.suguowen.sblog.service.DiscussionService;
import com.suguowen.sblog.service.HomeService;
import com.suguowen.sblog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 主页
 * @Author suguowen
 * @date 2019/4/9 14:23
 */
@Controller
public class HomeController {

    @Autowired
    HomeService show;
    @Autowired
    TagService tagService;
    @Autowired
    private Environment environment;
    @Autowired
    DiscussionService discussionService;

    /**
     * 文章列表查询（按标签查：！0 | 首页：0）
     * @token 日期 / 标签 / 全部
     * @pageNum 页码
     */
    @GetMapping("/home/{token}")
    public String toHome(@PathVariable("token") String token,
                         @RequestParam(value="pageNum",defaultValue="1",required=false) int pageNum,
                         Model model, HttpSession httpSession){
        // 是否登录
        User user = MySpringContext.getInstance().getUser(httpSession.getId());
        model.addAttribute("user",user);
        List<ArticlesVO> list = null; // 文章信息
        Integer tag_id = 0;// 当前标签
        List<Tag> tags = tagService.findAllTag();// 标签栏
        Integer currentPageNum = pageNum;// 当前页码
        List<String> dates = show.dateClassify();// 日期栏
        Integer pageSize = Integer.valueOf(environment.getProperty("pagehelper.page-size-zero"));
        List<CommonVO> comments = null;// 最新评论

        PageHelper.startPage(pageNum,pageSize);// 分页
        // 判断token是tag_id还是date
        if (token.contains("-")){
            list = show.getArticleByDate(token);// 日期分类的文章
            tag_id = 0;
        } else{
            tag_id = Integer.valueOf(token);
            if (tag_id == 0){
                list = show.getArticlesMsg();// 全部文章/主页
                // 最新评论
                comments = discussionService.findNewestCommons();
            }else{
                list = show.selectArticleByTag(tag_id);// 标签分类的文章
            }
        }

        // 构建model
        model.addAttribute("currentTag",tag_id);
        model.addAttribute("pageNum",currentPageNum);
        model.addAttribute("tags",tags);
        model.addAttribute("articles",list);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("arts_size",((Page)list).getTotal());// 总文章数
        model.addAttribute("dates",dates);
        model.addAttribute("comments",comments);
        // 获得最新评论 - delay

        return "home";
    }

}
